//
//  ActivityDetailsViewController.m
//  UI_豆瓣
//
//  Created by WangZe on 15/10/14.
//  Copyright © 2015年 王泽龙. All rights reserved.
//

#import "ActivityDetailsViewController.h"
#import "DatabaseHandle.h"
@interface ActivityDetailsViewController ()<UIAlertViewDelegate>
@property(nonatomic, retain)UIWebView *webView;

@end

@implementation ActivityDetailsViewController
{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:87 / 255.0 green:137 / 255.0 blue:157 / 255.0 alpha:1];
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.webView];
    NSURL *url = [NSURL URLWithString:self.strURL];
    NSLog(@"%@", self.strURL);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.title = self.act.title;
    
    SelectType result = [[DatabaseHandle shareDatabaseHandle] isHaveActivity:self.act];
    if (result == InTable)
    {
        
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"已收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction:)];
        
        }
        else if (result == NotInTable)
        {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction:)];
        }
    else
    {
        NSLog(@"出错了");
    }

    
}
- (void)collectAction:(UIBarButtonItem *)barButtonItem
{
    if ([barButtonItem.title isEqualToString:@"收藏"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏成功" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
        [alert show];
        [[DatabaseHandle shareDatabaseHandle] insertActivity:self.act];
        barButtonItem.title = @"已收藏";
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏,不需要重复收藏" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"取消收藏", nil];
        [alert show];
        
    }
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[DatabaseHandle shareDatabaseHandle] deleteActivity:self.act];
        self.navigationItem.rightBarButtonItem.title = @"收藏";
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
